// pages/indexAll/shiyanStep/shiyanStep.js
const app = getApp()
var self;
var id, loginid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startdate: "2019-07-11",
    enddate: "2019-07-11",
    starttime: "08:00",
    endtime: "08:30",
    weekday: [{ name: "周日", val: "0", disable: true },{ name: "周一", val: "1", disable: true }, { name: "周二", val: "2", disable: true }, { name: "周三", val: "3", disable: true }, { name: "周四", val: "4", disable: true }, { name: "周五", val: "5", disable: true }, { name: "周六", val: "6", disable: true } ],
    selectWeek:[],
    zhuti:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    self = this;
    id = options.id;
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    month = (month < 10 ? "0" + month : month);
    day = (day < 10 ? "0" + day : day);
    var nowDate = year + "-" + month + "-" + day;
    self.setData({
      startdate: nowDate,
      enddate:nowDate
    });
    loginid = app.checkLogin();
    this.checkWeek();
  },
  checkboxChange:function(e){
    this.setData({
      selectWeek: e.detail.value
    })
    
  },
  zhutiChange:function(e){
    this.setData({
      zhuti: e.detail.value
    }) 
    console.log(e.detail.value);
  },
  bindStartDateChange: function(e) {
    this.setData({
      startdate: e.detail.value
    })
    this.checkWeek();
  },
  bindStartTimeChange: function(e) {
    this.setData({
      starttime: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
    this.checkWeek();
  },
  bindEndTimeChange: function(e) {
    this.setData({
      endtime: e.detail.value
    })
    
  },
  checkWeek:function(e){
    var tmparr = this.getBetweenDateStr(self.data.startdate,self.data.enddate);
    var weekdata = this.data.weekday;
    var weekstr = "";
    for(var i=0;i<tmparr.length;i++){
      var weekday = new Date(tmparr[i]);
      weekstr += weekday.getDay()+",";
    }
    for(var i=0;i<weekdata.length;i++){
      if (weekstr.indexOf(weekdata[i].val)!=-1){
        weekdata[i].disable = false;
      }else{
        weekdata[i].disable = true;
      } 
    }
    self.setData({
      weekday:weekdata
    })
  },
  getBetweenDateStr(start, end) {
    if(start==end){
      return [start];
    }
    var result = []; 
    var beginDay = start.split("-");
    var endDay = end.split("-");
    var diffDay = new Date();
    var dateList = new Array;
    var i = 0;
    diffDay.setDate(beginDay[2]);
    diffDay.setMonth(beginDay[1] - 1);
    diffDay.setFullYear(beginDay[0]);
    result.push(start);
    while (i == 0) {
      var countDay = diffDay.getTime() + 24 * 60 * 60 * 1000;
      diffDay.setTime(countDay);
      dateList[2] = diffDay.getDate();
      dateList[1] = diffDay.getMonth() + 1;
      dateList[0] = diffDay.getFullYear();
      if (String(dateList[1]).length == 1) { dateList[1] = "0" + dateList[1] };
      if (String(dateList[2]).length == 1) { dateList[2] = "0" + dateList[2] };
      result.push(dateList[0] + "-" + dateList[1] + "-" + dateList[2]);
      if (dateList[0] == endDay[0] && dateList[1] == endDay[1] && dateList[2] == endDay[2]) {
        i = 1;
      }
    };
    return result;
  },
  submitIt: function (e) {
    loginid = app.checkLogin();
    if (!loginid) {
      return false;
    }
    //检测必须项
    if(this.data.selectWeek.length<=0){ 
      wx.showToast({
        title: '请选择星期',
        icon:'none'
      })
      return false;
    }
    wx.showModal({
      title: '提示',
      content: '是否确定预约该实验室？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'skyblue', //确定文字的颜色
      success: function (res) {
        if (!res.cancel) {
          self.goSubmit();
        }
      },
      fail: function (res) { }, //接口调用失败的回调函数
      complete: function (res) { }, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  goSubmit:function(e){
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=SaveShiYanShiYuYue",
      data: {
        ShiYanShiID: id,
        LoginID: loginid,
        KaiShiRiQi: this.data.startdate,
        JieShuRiQi: this.data.enddate,
        KaiShiShiDuan:this.data.starttime,
        JieShuShiDuan:this.data.endtime,
        YuYueZhou: this.data.selectWeek.join(",")

      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        } else if (res.data.state == -1) {
          app.goLogin();
        } else {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})